home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / grafica / amhelios / bitmap24.h < prev    next >
C/C++ Source or Header  |  1999-01-01  |  5KB  |  224 lines

  1. #ifndef BITMAP24_H
  2. #define BITMAP24_H
  3. /* -------------------------------------------------------------------------- *\
  4.    BITMAP24.H
  5.    Copyright © 1997 by Jarno van der Linden
  6.    jarno@kcbbs.gen.nz
  7.  
  8.    24 bit bitmap handling, including IFF24 saving
  9.    Header file
  10.  
  11.    This program is Freeware, and all usual Freeware rules apply.
  12.  
  13.    21 Feb 1997: Project started
  14. \* -------------------------------------------------------------------------- */
  15.  
  16. /* -------------------------------- Includes -------------------------------- */
  17. #include <exec/types.h>
  18. #include <dos/dos.h>
  19.  
  20. /* ------------------------------ Definitions ------------------------------- */
  21. enum {
  22.     BITMAP24_ERROR_NONE = 0,
  23.     BITMAP24_ERROR_NOBITMAP,
  24.     BITMAP24_ERROR_FILEOPEN,
  25.     BITMAP24_ERROR_OUTOFBOUNDS
  26. };
  27.  
  28. /* --------------------------------- Macros --------------------------------- */
  29.  
  30. /* -------------------------------- Typedefs -------------------------------- */
  31. typedef struct {
  32.     UBYTE r;
  33.     UBYTE g;
  34.     UBYTE b;
  35. } Colour;
  36.  
  37. /* ------------------------------ Proto Types ------------------------------- */
  38.  
  39. /* -------------------------------- Structs --------------------------------- */
  40. struct POINT    // Raster display point
  41. {
  42.   int x;        // X-axis co-ordinate
  43.   int y;        // Y-axis co-ordinate
  44. };
  45.  
  46. class BitMap24 {
  47.     public:
  48.         BitMap24(UWORD width,UWORD height);
  49.         ~BitMap24();
  50.         int GetError();
  51.         BOOL HasError();
  52.         char *GetErrorStr();
  53.         UWORD GetWidth();
  54.         UWORD GetHeight();
  55.         void GetColour(Colour *c,int x,int y);
  56.         UBYTE GetRed(int x,int y);
  57.         UBYTE GetGreen(int x,int y);
  58.         UBYTE GetBlue(int x,int y);
  59.         void SetColour(UBYTE r,UBYTE g,UBYTE b,int x,int y);
  60.  
  61.         inline UWORD GetWidthFast();
  62.         inline UWORD GetHeightFast();
  63.         inline void GetColourFast(Colour *c,int x,int y);
  64.         inline UBYTE GetRedFast(int x,int y);
  65.         inline UBYTE GetGreenFast(int x,int y);
  66.         inline UBYTE GetBlueFast(int x,int y);
  67.         inline void SetColourFast(UBYTE r,UBYTE g,UBYTE b,int x,int y);
  68.  
  69.         void WriteBitMap(char *file);
  70.     protected:
  71.         inline BOOL BoundsCheck(int x,int y);
  72.         inline void WriteV(BPTR fh,ULONG v);
  73.         inline void WriteB(BPTR fh,UBYTE b);
  74.         inline void WriteC(BPTR fh, BYTE b);
  75.         inline void WriteP(BPTR fh,void *p,int size);
  76.         inline void WriteS(BPTR fh,char *s);
  77.         inline UBYTE GatherBits(int row, int c, int b, int x);
  78.         inline void WriteRun(BPTR fh, int row, int c, int b, int runstart, int runend);
  79.         inline void WriteDump(BPTR fh, int row, int c, int b, int runstart, int runend);
  80.         inline void FindRun(int row, int c, int b, int start, int *runstart, int *runlength);
  81.     protected:
  82.         inline Colour *GetPixelPtr(int x,int y);
  83.     protected:
  84.         Colour *bitmap;
  85.         UWORD width,height;
  86.         int bytewidth;
  87.         int error;
  88.  
  89.     friend class WinBitmap;
  90. };
  91.  
  92. #include "color.h"
  93. class WinBitmap
  94. {
  95.     public:
  96.         WinBitmap()
  97.         {
  98.             bitmap = NULL;
  99.         }
  100.         ~WinBitmap()
  101.         {
  102.             ;
  103.         }
  104.         BOOL Open(int w,int h)
  105.         {
  106.             if(bitmap)
  107.             {
  108.                 delete bitmap;
  109.             }
  110.  
  111.             bitmap = new BitMap24((UWORD)w,(UWORD)h);
  112.  
  113.             return !(bitmap->HasError());
  114.         }
  115.         void Close()
  116.         {
  117.             if(bitmap)
  118.                 delete bitmap;
  119.         }
  120.         void GetPixel(int x,int y, ColorRGB *pc)
  121.         {
  122.             Colour c;
  123.             bitmap->GetColourFast(&c,x,y);
  124.             pc->SetBlue((BYTE)(c.b));
  125.             pc->SetGreen((BYTE)(c.g));
  126.             pc->SetRed((BYTE)(c.r));
  127.         }
  128.         void SetPixel(int x,int y,ColorRGB &c)
  129.         {
  130.             UBYTE r,g,b;
  131.             b = (UBYTE)(c.GetBlue());
  132.             g = (UBYTE)(c.GetGreen());
  133.             r = (UBYTE)(c.GetRed());
  134.             bitmap->SetColourFast(r,g,b,x,y);
  135.         }
  136.         int GetWidth()
  137.         {
  138.             return (int)(bitmap->GetWidthFast());
  139.         }
  140.         int GetHeight()
  141.         {
  142.             return (int)(bitmap->GetHeightFast());
  143.         }
  144.         void SetPixelPtr(int x,int y)
  145.         {
  146.             curp = bitmap->GetPixelPtr(x,y);
  147.         }
  148.         void SetCurrPixel(ColorRGB &c)
  149.         {
  150.             curp->b = c.GetBlue();
  151.             curp->g = c.GetGreen();
  152.             curp->r = c.GetRed();
  153.         }
  154.         void IncPixelPtr()
  155.         {
  156.             curp++;
  157.         }
  158.         BOOL Write(char *file)
  159.         {
  160.             bitmap->WriteBitMap(file);
  161.             return !(bitmap->HasError());
  162.         }
  163.     protected:
  164.         BitMap24 *bitmap;
  165.         Colour *curp;
  166. };
  167.  
  168. /* -------------------------------- Globals --------------------------------- */
  169.  
  170. /* ---------------------------------- Code ---------------------------------- */
  171. UWORD BitMap24::GetWidthFast()
  172. {
  173.     return width;
  174. }
  175.  
  176.  
  177. UWORD BitMap24::GetHeightFast()
  178. {
  179.     return height;
  180. }
  181.  
  182.  
  183. void BitMap24::GetColourFast(Colour *c,int x,int y)
  184. {
  185.     *c = bitmap[y*width+x];
  186. }
  187.  
  188.  
  189. UBYTE BitMap24::GetRedFast(int x,int y)
  190. {
  191.     return bitmap[y*width+x].r;
  192. }
  193.  
  194.  
  195. UBYTE BitMap24::GetGreenFast(int x,int y)
  196. {
  197.     return bitmap[y*width+x].g;
  198. }
  199.  
  200.  
  201. UBYTE BitMap24::GetBlueFast(int x,int y)
  202. {
  203.     return bitmap[y*width+x].b;
  204. }
  205.  
  206.  
  207. void BitMap24::SetColourFast(UBYTE r,UBYTE g,UBYTE b,int x,int y)
  208. {
  209.     Colour *bp;
  210.  
  211.     bp = &(bitmap[y*width+x]);
  212.     bp->r = r;
  213.     bp->g = g;
  214.     bp->b = b;
  215. }
  216.  
  217.  
  218. Colour *BitMap24::GetPixelPtr(int x,int y)
  219. {
  220.     return &(bitmap[y*width+x]);
  221. }
  222.  
  223. #endif /* BITMAP24_H */
  224.